//
//  WLLEmoticonKeyboardView.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class WLLEmoticonKeyboardView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    func setupUI()  {
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        //添加控件
        //底部视图
        addSubview(emoticonBottomView)
        //表情视图
        addSubview(emoticonPageView)
        //pageControl
        addSubview(emoticonPageControl)
        //添加约束
        emoticonBottomView.snp_makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
        emoticonPageView.snp_makeConstraints { (make) in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(emoticonBottomView.snp_top)
        }
        emoticonPageControl.snp_makeConstraints { (make) in
            make.bottom.equalTo(emoticonPageView).offset(5)
            make.centerX.equalTo(emoticonPageView)
        }
        /**
         *  底部视图闭包回调
         */
        emoticonBottomView.emoBottom_closure = { (type:WLLEmoticonBottomViewType)->() in
            switch type {
            case .Default:
                print(WLLEmoticonBottomViewType.Default.rawValue,"默认")
            case .Emoji:
                print(WLLEmoticonBottomViewType.Emoji.rawValue,"emoji")
            case .Lxh:
                print(WLLEmoticonBottomViewType.Lxh.rawValue,"浪小花")
            }
            //点击按钮 pageview 滚动到对应的界面
            //获取 indexpaht
            let indexPaht = NSIndexPath(forItem: 0, inSection: type.rawValue)
            //TODO:实际微博没有高亮状态
            
            //滚动到指定位置
            //TODO:设置了动画
            self.emoticonPageView.scrollToItemAtIndexPath(indexPaht, atScrollPosition:UICollectionViewScrollPosition.Left, animated: false)
            
            //底部滚动,联动 pageControl
            self.setPageControl(indexPaht)
            
        }
        setPageControl(NSIndexPath(forItem: 0, inSection: 0))
        
    }
    
    
    
    
    //MARK: - 懒加载控件
    /// 底部视图
    //    @available(iOS 9.0, *)
    private lazy var emoticonBottomView : WLLEmoticonBottomView = WLLEmoticonBottomView(frame: CGRectZero)
    ///表情视图
    private lazy var emoticonPageView : WLLEmoticonPageView = {
        let view = WLLEmoticonPageView()
        view.backgroundColor = self.backgroundColor
        view.bounces = false
        view.pagingEnabled = true
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.delegate = self
        return view
    }()
    ///pageControl
    private lazy var emoticonPageControl : UIPageControl = {
        let view = UIPageControl()
//        view.pageIndicatorTintColor = UIColor(patternImage:UIImage(named:"compose_keyboard_dot_normal")!)
        view.setValue(UIImage(named:"compose_keyboard_dot_normal"), forKey: "pageImage")
        
        view.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        //用户交互
       return view
    }()
   
    
}
//MARK: - 代理,本来 WLLEmoticonKeyboardView 继承的 uiview. 可是遵循 UICollectionViewDelegate 有点萌
extension WLLEmoticonKeyboardView : UICollectionViewDelegate{
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //获取屏幕的中心点(坐标是相对于pageView的)
        let centerX = KSCREENW*0.5 + scrollView.contentOffset.x
        //获取屏幕显示的 cells(因为系统没有排序,需要我们自己排).....我没有写 self
        let cells = emoticonPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        //如果屏幕上出现两个 cell 我们才会判断
        if cells.count == 2 {
            //第一个 cell
            let firstCell = cells.first!
            //第二个 cell
            let lastCell = cells.last!
            
            //定义一个 indexPath
            let indexPath : NSIndexPath
            
            //判断哪个 cell 包含"中心点"
            if firstCell.frame.contains(CGPoint(x: centerX, y: 1)) {
                
                indexPath = emoticonPageView.indexPathForCell(firstCell)!
            } else {
                indexPath = emoticonPageView.indexPathForCell(lastCell)!
            }
            
            //设置对应按钮的选中
            emoticonBottomView.setupCurrentButton(indexPath.section)
            
            setPageControl(indexPath)
        }
        
        
        
    }
    //MARK: - 设置 pageControl
    func setPageControl (indexPath : NSIndexPath)  {
        //总页数
        emoticonPageControl.numberOfPages = WLLEmoticonTools.sharedTools.allEmoticons[indexPath.section].count
        //选择的页数
        emoticonPageControl.currentPage = indexPath.item
    }
    
}






